function get_dB() {
    // 获取分贝值
    dB = textContains("dB").findOne().text();  // 找到含有“dB”标签控件
    return parseInt(dB.replace("d", "").replace("B", ""));  // 替换掉 “dB” 然后返回分贝值
}

function get_time() {
    var newDate = new Date();
    return newDate.getHours().toString() + ":" + newDate.getMinutes().toString() + ":" +  newDate.getSeconds().toString();
}
console.log(get_time())

function get_mean(arr) {
    var len = arr.length;
    var sum = 0;
    for(var i = 0;i<len;i++){
        sum +=arr[i];
    }
    return sum/len;
}


history_dB = Array()

setInterval(
    function () {
        time = get_time()
        if (parseInt(time.substring(time.lastIndexOf(":") + 1))<=5 && parseInt(time.substring(time.lastIndexOf(":") + 1))>=0 && history_dB.length >= 10) {
            avg = get_mean(history_dB)
            console.log("写出分贝值 " + avg.toString())
            files.append("/sdcard/comfire.csv", get_time().substring(0, time.lastIndexOf(":")) + ":00," + avg.toString() + "\n");
            history_dB.length = 0;
        } else {
            history_dB.push(get_dB())
        }
    }
, 1000)
